//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: '小店之家',
    userInfo: {},
    balance: '',
    money: '',
    error: false,
    errorMsg: ''
  },
  bindScanCode: function() {
    app.bindScanCode();
  },
  bindKeyInput: function(e) {
    this.setData({
      money: e.detail.value
    })
    this._validate(e.detail.value);
  },
  drawAll: function() {
    this.setData({
      money: this.data.balance
    });
    this._validate(this.data.balance);
  },

  _validate: function(val){
    if(!val) {
      this.setData({
        error: true,
        errorMsg: '请输入提现金额！'
      });
      return false;
    }
    var reg = /^\d+(\.\d+)?$/;
    if(!reg.test(val)) {
      this.setData({
        error: true,
        errorMsg: '请输入有效的金额格式！'
      });
      return false;
    }
    if((val - this.data.balance) > 0){
      this.setData({
        error: true,
        errorMsg: '提现金额超过收入金额！'
      });
      return false;
    }
    if(val < 1) {
      this.setData({
        error: true,
        errorMsg: '提现金额不能小于1元！'
      });
      return false;
    }
    this.setData({
      error: false
    });
    return true;
  },
  navigatorPhone: function(){
    if(this._validate(this.data.money)) {
      this.setData({
        error: false
      });
      wx.navigateTo({
        url: '../phoneverify/phoneverify?money=' + this.data.money
      })
    }
  },
  onLoad: function (options) {
    if(options.balance) {
      this.setData({
        balance:options.balance
      });
    }
    var that = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  }
})
